﻿using LdtJudge.DataEngine.Common.CommHelp;
using LdtJudge.DataEngine.Common.StringAssemble;
using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using LdtJudge.DataEngine.Model.DataModel.SystemtableModel;
using LdtJudge.DataEngine.Model.ElmentModel;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;

namespace LdtDataEngin.ModuleDetail.BaseModule
{
    public class ElementConfig
    {
        private static ElementConfig _elementConfig;
        /// <summary>
        /// 获取实例对象
        /// </summary>
        public static ElementConfig InstantiationModel
        {
            get
            {
                return _elementConfig = _elementConfig ?? new ElementConfig();
            }

        }
        /// <summary>
        /// 案由处理类
        /// </summary>
        private GetSysCaseCause dalsys_casecause;

        /// <summary>
        /// 初始化案由配置数据
        /// </summary>
        public ElementConfig()
        {
            dalsys_casecause = new GetSysCaseCause();
        }

        /// <summary>
        /// 返回案由对象
        /// </summary>
        /// <param name="CC_ID">案由名称或者案由ID</param>
        /// <param name="caseType">案件类型</param>
        /// <returns></returns>
        public CaseCauseModel GetCaseCauseModel(string CC_ID, string caseType)
        {
            return dalsys_casecause.GetModelByCauseName(CC_ID, caseType);
        }

        /// <summary>
        /// 返回案由对象
        /// </summary>
        /// <param name="CC_ID">案由ID</param>
        /// <returns></returns>
        public CaseCauseModel GetCaseCauseModel(string CC_ID)
        {
            return dalsys_casecause.GetModel(CC_ID);
        }
        /// <summary>
        /// 根据案由ID获取所有的案由下要素节点信息
        /// </summary>
        /// <param name="CC_ID">案由ID</param>
        /// <returns>返回案由对象集合</returns>
        public IList<CaseCauseModel> GetAllFact_ElementList(string CC_ID)
        {
            IList<CaseCauseModel> mcauseObjList = dalsys_casecause.GetList_FactElement(CC_ID);
            return mcauseObjList;
        }

        /// <summary>
        /// 初始化匹配关键词的字典值。
        /// </summary>
        /// <param name="causeID">案由</param>
        public Dictionary<string, ElementConfigModel> InitElementParamter(string causeID)
        {
            Dictionary<string, ElementConfigModel> dickeyvalues = new Dictionary<string, ElementConfigModel>();
            List<SYS_ArgumentType> oTb = dalsys_casecause.GetArgumentTypeByAt_CauseID( causeID);
            CaseCauseModel mCause = dalsys_casecause.GetModel(causeID);

            #region 注释
            //XmlDocument doc = new XmlDocument();
            //Dictionary<string, string> configXmlPath = new Dictionary<string, string>();
            //try
            //{
            //    doc.LoadXml(mCause.ConfigXML);
            //    XmlNodeList nodes = doc.SelectNodes("/elementConfig/element/sourceContent");
            //    foreach (XmlNode node in nodes)
            //    {
            //        if (node != null)
            //        {
            //            if ((!configXmlPath.ContainsKey(node.InnerText.Trim())) && (!string.IsNullOrEmpty(node.InnerText.Trim())))
            //            {
            //                //解决重复添加KEY值的问题  郭晓强
            //                if (!configXmlPath.ContainsKey(node.ParentNode.Attributes[0].Value))
            //                {
            //                    configXmlPath.Add(node.ParentNode.Attributes[0].Value, node.InnerText.Trim());
            //                }
            //            }
            //        }
            //    }
            //}
            //catch (Exception err)
            //{ 
            //}
            //foreach (DataRow orow in oTb.Rows)
            //{
            //    string elementID = LdFormat.ObjectToString(orow["AT_Code"]);
            //    string matchKeyWords = LdFormat.ObjectToString(orow["AT_MatchKeyWord"]);
            //    var allkey = configXmlPath.Where(a => a.Value == elementID).ToList();//按照关联键查找所有的xml路径

            //    foreach (var item in allkey)//循环添加
            //    {
            //        if (!elementParamterDict.ContainsKey(item.Key))//判断是否已添加
            //        {
            //            elementParamterDict.Add(item.Key, matchKeyWords);
            //        }
            //    }
            //}


            #endregion

            if (!string.IsNullOrWhiteSpace(mCause.ConfigXML))
            {
                XmlDocument oDom = new XmlDocument();
                oDom.LoadXml(mCause.ConfigXML);

                XmlNodeList list = oDom.SelectNodes("/elementConfig/element");
                foreach (XmlNode oNode in list)
                {
                    ElementConfigModel newconfig = new ElementConfigModel();

                    #region 新属性值加载

                    newconfig.isNodeByAtticton = "1";
                    foreach (XmlNode node in oNode.ChildNodes)
                    {
                        if (!newconfig.DictionaryAttribute.ContainsKey(node.Name))
                        {
                            newconfig.DictionaryAttribute.Add(node.Name, node.Value ?? "");
                        }
                    }

                    #endregion


                    string xmlPath = oNode.Attributes["xmlPath"].Value;
                    xmlPath = Regex.Replace(xmlPath, "^(?!/)", "/");        //假如配置的路径不包含开始符"/"时，则追加上。

                    newconfig.XmlPath = xmlPath;

                    XmlNode AttributeNode = oNode.SelectSingleNode("checkType"); //单行提取
                    newconfig.checkType = (AttributeNode == null) ? "radio" : AttributeNode.InnerText;

                    AttributeNode = oNode.SelectSingleNode("sourceType");//数据源类型
                    newconfig.sourceType = (AttributeNode == null) ? "" : AttributeNode.InnerText;

                    AttributeNode = oNode.SelectSingleNode("defaultValue");//默认值
                    newconfig.defaultValue = (AttributeNode == null) ? "" : AttributeNode.InnerText;

                    AttributeNode = oNode.SelectSingleNode("isSerialField");//
                    newconfig.isSerialField = (AttributeNode == null) ? 0 : LdFormat.ObjectToInt(AttributeNode.InnerText);

                    AttributeNode = oNode.SelectSingleNode("fieldType");//字段类型
                    newconfig.fieldType = (AttributeNode == null) ? "varchar" : AttributeNode.InnerText;

                    AttributeNode = oNode.SelectSingleNode("formatType");//是否格式化
                    if (AttributeNode != null)
                    {
                        newconfig.formatType = (AttributeNode == null) ? "" : AttributeNode.InnerText;
                    }


                    AttributeNode = oNode.SelectSingleNode("suitDegreeID");//诉讼地位
                    newconfig.suitDegreeID = (AttributeNode == null) ? "" : LdFormat.ObjectToString(AttributeNode.InnerText);

                    AttributeNode = oNode.SelectSingleNode("matchPersonMode");//人员信息匹配
                    newconfig.matchPersonMode = (AttributeNode == null) ? "" : LdFormat.ObjectToString(AttributeNode.InnerText);

                    AttributeNode = oNode.SelectSingleNode("matchPersonCondi");//人员匹配类型
                    newconfig.matchPersonCondi = (AttributeNode == null) ? "" : LdFormat.ObjectToString(AttributeNode.InnerText);

                    AttributeNode = oNode.SelectSingleNode("isAloneExtract");//自定义提取
                    newconfig.isAloneExtract = (AttributeNode == null) ? 0 : LdFormat.ObjectToInt(AttributeNode.InnerText);

                    AttributeNode = oNode.SelectSingleNode("isMultiExact");//多行
                    newconfig.isMultiExact = (AttributeNode == null) ? 0 : LdFormat.ObjectToInt(AttributeNode.InnerText);

                    AttributeNode = oNode.SelectSingleNode("sourceContent");//
                    string sourceArgument = (AttributeNode == null) ? "" : LdFormat.ObjectToString(AttributeNode.InnerText);
                    if (AttributeNode != null && !string.IsNullOrWhiteSpace(AttributeNode.InnerText))
                    {
                        var dataRows = oTb.Where(a => a.AT_Code == sourceArgument);
                        if (dataRows != null && dataRows.Count() > 0)
                        {
                            newconfig.sourceContent = sourceArgument;
                        }
                    }

                    if (!dickeyvalues.ContainsKey(xmlPath))
                        dickeyvalues.Add(xmlPath, newconfig);
                }

                //添加对要素结点的配置
                XmlNodeList nodelist = oDom.SelectNodes("/elementConfig/node");
                foreach (XmlNode oNode in nodelist)
                {
                    ElementConfigModel cnodeDefine = new ElementConfigModel();

                    #region 新属性值添加

                    cnodeDefine.isNodeByAtticton = "0";
                    foreach (XmlNode node in oNode.ChildNodes)
                    {
                        if (!cnodeDefine.DictionaryAttribute.ContainsKey(node.Name))
                        {
                            cnodeDefine.DictionaryAttribute.Add(node.Name, node.Value);
                        }

                    }

                    #endregion

                    string xmlPath = oNode.Attributes["xmlPath"].Value;
                    xmlPath = Regex.Replace(xmlPath, "^(?!/)", "/");        //假如配置的路径不包含开始符"/"时，则追加上。
                    cnodeDefine.XmlPath = xmlPath;

                    XmlNode nAttrNode = oNode.SelectSingleNode("isPersonNode");
                    cnodeDefine.isPersonNode = (nAttrNode == null) ? 0 : LdFormat.ObjectToInt(nAttrNode.InnerText);

                    nAttrNode = oNode.SelectSingleNode("suitDegreeID");
                    cnodeDefine.suitDegreeID = (nAttrNode == null) ? "" : LdFormat.ObjectToString(nAttrNode.InnerText);

                    nAttrNode = oNode.SelectSingleNode("matchPersonMode");
                    cnodeDefine.matchPersonMode = (nAttrNode == null) ? "" : LdFormat.ObjectToString(nAttrNode.InnerText);

                    nAttrNode = oNode.SelectSingleNode("matchPersonCondi");
                    cnodeDefine.matchPersonCondi = (nAttrNode == null) ? "" : LdFormat.ObjectToString(nAttrNode.InnerText);

                    nAttrNode = oNode.SelectSingleNode("isAloneExtract");
                    cnodeDefine.isAloneExtract = (nAttrNode == null) ? 0 : LdFormat.ObjectToInt(nAttrNode.InnerText);

                    nAttrNode = oNode.SelectSingleNode("isMultiExact");
                    cnodeDefine.isMultiExact = (nAttrNode == null) ? 0 : LdFormat.ObjectToInt(nAttrNode.InnerText);

                    nAttrNode = oNode.SelectSingleNode("parentnode");
                    cnodeDefine.parentnode = (nAttrNode == null) ? "" : LdFormat.ObjectToString(nAttrNode.InnerText);

                    XmlNode AttributeNode = oNode.SelectSingleNode("checkType"); //单行提取
                    cnodeDefine.checkType = (AttributeNode == null) ? "radio" : AttributeNode.InnerText;

                    //2016-11-24 fyb 用于推原告
                    nAttrNode = oNode.SelectSingleNode("plaintiff");
                    cnodeDefine.plainTiff = (nAttrNode == null) ? 0 : LdFormat.ObjectToInt(nAttrNode.InnerText);
                    if (!dickeyvalues.ContainsKey(xmlPath))
                        dickeyvalues.Add(xmlPath, cnodeDefine);
                }
            }
            return dickeyvalues;
        }
    }
}
